package Aleehot100;

import java.util.*;

/**
 * ClassName: c019
 * Package: Aleehot100
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/5/30 14:52
 * @Version 1.0
 * @Since 1.0
 */
public class c019 {
    public static void main(String[] args) {
        int[][] arr = {{1,2,3,4},
                        {5,6,7,8},
                        {9,10,11,12}};
        int[][] arr2 = {{7},
                        {9},
                        {6}};
        System.out.println(spiralOrder(arr2));
    }


    /**
     * 螺旋矩阵遍历方法
     *
     * @param matrix
     * @return
     */
    public static List<Integer> spiralOrder(int[][] matrix) {
        int top = 0, bottom = matrix.length - 1, left = 0, right = matrix[0].length - 1;
        ArrayList<Integer> res = new ArrayList<>();
        while (top <= bottom && left <= right) {
            // 顶部
            for (int i = left; i <= right; i++) {
                res.add(matrix[top][i]);
            }
            // 右
            for (int i = top + 1; i <= bottom; i++) {
                res.add(matrix[i][right]);
            }

            // 下
            if(top != bottom) {
                for (int i = right - 1; i >= left; i--) {
                    res.add(matrix[bottom][i]);
                }
            }

            // 左
            if(left != right) {
                for (int i = bottom - 1; i > top; i--) {
                    res.add(matrix[i][left]);
                }
            }

            top++;
            bottom--;
            left++;
            right--;
        }

        return res;
    }

}
